﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace YEBHI.MVC.App_Start.Yebhi_ViewEngies
{
    public class yebhiViewEngine : RazorViewEngine
    {
        public yebhiViewEngine()
        {
            // Define the location of the View file
            //0-Action, 1- Controller, 2- Area
            this.ViewLocationFormats = new string[] { "~/Views/{0}.cshtml"};
            this.PartialViewLocationFormats = new string[] { "~/Views/{0}.cshtml" };
            this.MasterLocationFormats = new string[] { "~/Views/{0}.cshtml" };
            this.AreaViewLocationFormats = new string[] {"~/Views/{0}.cshtml" };
            this.AreaMasterLocationFormats = new string[] { "~/Views/{0}.cshtml" };
            this.AreaPartialViewLocationFormats = new string[] { "~/Views/{0}.cshtml" };
            
        }

        protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
        {

            return base.CreatePartialView(controllerContext, partialPath);
        }

       /* protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
        {
            if (controllerContext.HttpContext.Request.Url.Host == "localhost")
            {
                string strpath = viewPath.Remove(0, "~/Views/".Length);
                //masterPath = masterPath.Remove(0, "~/Views/".Length);
                viewPath = "~/Views/Yebhi/" + strpath;
                //masterPath = "~/Views/Yebhi/" + masterPath;
            }
            return base.CreateView(controllerContext, viewPath, masterPath);

        }*/
        public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
        {
            string strAreaName = Convert.ToString(controllerContext.RouteData.Values["Area"]);
            strAreaName = (strAreaName != string.Empty ? strAreaName + "/" : string.Empty);
            HttpRequestBase oHttpRequestBase = controllerContext.HttpContext.Request;
            string strControllerName = controllerContext.Controller.GetType().Name.Replace("Controller","");
            
            switch (oHttpRequestBase.Url.Host)
            {
                case "localhost":
                    {
                        viewName = "Yebhi/"+strAreaName + strControllerName +"_"+ viewName;
                    }
                    break;
                default:
                    {
                        viewName = strAreaName+strControllerName +"_"+ viewName;
                    }
                    break;
            }
           
             
            //controllerContext.RequestContext.HttpContext.Request.Cookies["ApplicatonTemplateDir"].ToString();
            return base.FindView(controllerContext, viewName, masterName, useCache);
        }
        public override ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache)
        {
            string strAreaName = Convert.ToString(controllerContext.RouteData.Values["Area"]);
            strAreaName = (strAreaName != string.Empty ? strAreaName + "/" : string.Empty);
            HttpRequestBase oHttpRequestBase = controllerContext.HttpContext.Request;
            string strControllerName = controllerContext.Controller.GetType().Name.Replace("Controller", "");

            switch (oHttpRequestBase.Url.Host)
            {
                case "localhost":
                    {
                        partialViewName = "Yebhi/" + strAreaName + strControllerName + "_" + partialViewName;
                    }
                    break;
                default:
                    {
                        partialViewName = strAreaName + strControllerName + "_" + partialViewName;
                    }
                    break;
            }
           
             
            return base.FindPartialView(controllerContext, partialViewName, useCache);
        }
       /* protected override bool FileExists(ControllerContext controllerContext, string virtualPath)
        {
            if (controllerContext.HttpContext.Request.Url.Host == "localhost")
            {
                string strpath = virtualPath.Remove(0, "~/Views/".Length);
                virtualPath = "~/Views/Yebhi/" + strpath;
            }
            return base.FileExists(controllerContext, virtualPath);

        }*/
    }
}
